package com.zenith.storage.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.PageResult;
import com.zenith.common.result.Result;
import com.zenith.storage.po.dto.FolderDTO;
import com.zenith.storage.po.dto.FolderFileQueryDTO;
import com.zenith.storage.po.dto.MoveFileDTO;
import com.zenith.storage.po.dto.RenameDTO;
import com.zenith.storage.service.NetDiskService;
import com.zenith.storage.po.vo.FolderFileVO;
import com.zenith.storage.po.vo.FolderVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 网盘功能控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/storage/netdisk")
@RequiredArgsConstructor
public class NetDiskController {
    
    private final NetDiskService netDiskService;
    
    /**
     * 创建文件夹
     */
    @SecurePostMapping("/createFolder")
    public Result<FolderVO> createFolder(@Valid @RequestBody FolderDTO folderDTO) {
        try {
            FolderVO result = netDiskService.createFolder(folderDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("创建文件夹失败", e);
            return Result.fail("创建文件夹失败");
        }
    }
    
    /**
     * 获取文件夹树
     */
    @SecurePostMapping("/getFolderTree")
    public Result<List<FolderVO>> getFolderTree(@RequestParam Long userId) {
        try {
            List<FolderVO> result = netDiskService.getFolderTree(userId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取文件夹树失败", e);
            return Result.fail("获取文件夹树失败");
        }
    }
    
    /**
     * 获取文件夹中的文件列表
     */
    @SecurePostMapping("/getFolderFiles")
    public Result<PageResult<FolderFileVO>> getFolderFiles(FolderFileQueryDTO queryDTO) {
        try {
            PageResult<FolderFileVO> result = netDiskService.getFolderFiles(queryDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取文件夹文件列表失败", e);
            return Result.fail("获取文件夹文件列表失败");
        }
    }
    
    /**
     * 移动文件
     */
    @SecurePostMapping("/moveFile")
    public Result<Boolean> moveFile(@Valid @RequestBody MoveFileDTO moveFileDTO) {
        try {
            Boolean result = netDiskService.moveFile(moveFileDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("移动文件失败", e);
            return Result.fail("移动文件失败");
        }
    }
    
    /**
     * 重命名文件或文件夹
     */
    @SecurePostMapping("/rename")
    public Result<Boolean> rename(@Valid @RequestBody RenameDTO renameDTO) {
        try {
            Boolean result = netDiskService.rename(renameDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("重命名失败", e);
            return Result.fail("重命名失败");
        }
    }
}